package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱城委 on 2021/7/8.<br><br>
 *
 * 快乐数
 */
public class Topic202 {

    public static void main(String[] args) {
        Topic202 instance = new Topic202();

        System.out.println(instance.isHappy(19));
        System.out.println(instance.isHappy(2));
        System.out.println(instance.isHappy(7));
    }

    public boolean isHappy(int n) {
        List<Integer> list = new ArrayList<>();

        int result = n;
        while (result != 1) {
            String number = String.valueOf(result);

            result = 0;
            for(int i = 0; i < number.length(); i++) {
                int value = number.charAt(i) - '0';
                result += value * value;
            }

            if(list.contains(result)) {
                return false;
            }

            list.add(result);
        }

        return true;
    }
}
